cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(recipe-05 LANGUAGES CXX)

option(USE_LIBRARY "Compile sources into library" OFF)                      # 用户显示选项,-D选项 -DUSE_LIBRARY
message(STATUS "Compile sources into library? ${USE_LIBRARY}")

include(CMakeDependentOption)   # cmake_dependent_option的头文件
cmake_dependent_option(MAKE_STATIC_LIBRARY "Compile sources into library?" OFF "USE_LIBRARY" ON)    # 若USE_LIBRARY为ON，则MAKE_STATIC_LIBRARY为OFF
cmake_dependent_option(MAKE_SHARED_LIBRARY "Compile sources into library?" ON "USE_LIBRARY" ON)     # 若USE_LIBRARY为ON，则MAKE_SHARED_LIBRARY为OFF

list(APPEND _sources Message.hpp Message.cpp)       # _sources表示后面的vector<string>

if (USE_LIBRARY)
    message(STATUS "Compile sources into a STATIC library? ${MAKE_STATIC_LIBRARY}")
    message(STATUS "Compile sources into a SHARED library? ${USE_LIBRARY}")
    if (MAKE_STATIC_LIBRARY)                                                # 制作静态库
        add_library(message STATIC ${_sources})
        add_executable(hello-world hello-world.cpp)
        target_link_libraries(hello-world message)
    elseif (MAKE_SHARED_LIBRARY)                                            # 制作动态库
        add_library(message SHARED ${_sources})
        add_executable(hello-world hello-world.cpp)
        target_link_libraries(hello-world message)
    endif ()
else ()
    add_executable(hello-world hello-world.cpp ${_sources})                 # USE_LIBRARY 为false，直接用源文件编译
endif ()